package com.hkts.naturalgas.alarminformation.controller;

import com.hkts.naturalgas.alarminformation.entity.NaturalgasGateStation;
import com.hkts.naturalgas.alarminformation.service.INaturalgasGateStationService;
import com.hkts.naturalgas.utils.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassNameNaturalgasAataAnalysis
 * @Description 门站数据同比分析
 * @Author yck
 * @Date2020-09-17 11:25
 * @Version V1.0
 **/
@RestController
@RequestMapping("/instrumentdata/naturalgasAataAnalysis")
public class NaturalgasAataAnalysis {
    @Autowired
    public INaturalgasGateStationService iNaturalgasGateStationService;


    @GetMapping("/getAnalysisData")
    @ResponseBody
    public Result<Object> getAnalysisData(String category, String dateRange, String[] dateArray, String isAllGate) {
        Map<String, Object> map = new HashMap<String, Object>();
        NaturalgasGateStation naturalgasGateStation = new NaturalgasGateStation();
        List<NaturalgasGateStation> naturalgasGateStations = iNaturalgasGateStationService.selectList(naturalgasGateStation);
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
        float maxValue = 0;
        float minValue = 0;
        float totalValue = 0;
        //近七天累计用量
        float cumulativeData = 0;
        cumulativeData = TestDataUtils.getDateData() * 7;
        map.put("cumulativeData", cumulativeData);
        if (category.equals("0")) {
            if (dateRange.equals("0")) {
                map.put("hoursArray", TestDataUtils.getListHours(dd.format(TestDataUtils.getYesterday())));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 24; j++) {
                        float hoursData = TestDataUtils.getHoursData();
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
            }
            //近7天
            else if (dateRange.equals("1")) {
                //时间集合
                map.put("hoursArray", TestDataUtils.getDateArray(7));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 7; j++) {
                        float hoursData = TestDataUtils.getHoursData() * 24;
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
                map.put("cumulativeData", map.get("totalValue"));
            } else if (dateRange.equals("2")) {
                map.put("hoursArray", TestDataUtils.getDateArray(30));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 30; j++) {
                        float hoursData = TestDataUtils.getHoursData() * 24;
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
            } else if (dateRange.equals("3")) {
                List<String> daysByTime = TestDataUtils.getDaysByTime(dateArray);
                map.put("hoursArray", daysByTime);
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < daysByTime.size(); j++) {
                        float hoursData = TestDataUtils.getHoursData() * 24;
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
            }
        } else if (category.equals("1")) {
            if (dateRange.equals("0")) {
                List<String> listHours = new ArrayList<>();
                listHours.add(dd.format(TestDataUtils.getYesterday()));
                map.put("hoursArray", listHours);
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 24; j++) {
                        float hoursData = TestDataUtils.getHoursData();
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
            } else if (dateRange.equals("1")) {
                map.put("hoursArray", TestDataUtils.getDateArray(7));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 7; j++) {
                        float hoursData = TestDataUtils.getHoursData() * 24;
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
                map.put("cumulativeData", map.get("totalValue"));
            } else if (dateRange.equals("2")) {
                map.put("hoursArray", TestDataUtils.getDateArray(30));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 30; j++) {
                        float hoursData = TestDataUtils.getHoursData() * 24;
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
                map.put("cumulativeData", map.get("totalValue"));
            } else if (dateRange.equals("3")) {
                List<String> daysByTime = TestDataUtils.getDaysByTime(dateArray);
                map.put("hoursArray", daysByTime);
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < daysByTime.size(); j++) {
                        float hoursData = TestDataUtils.getHoursData() * 24;
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
            }
        } else if (category.equals("2")) {
            if (dateRange.equals("0")) {
                List<String> listHours = new ArrayList<>();
                listHours.add(dd.format(TestDataUtils.getYesterday()));
                map.put("hoursArray", listHours);
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 24; j++) {
                        float hoursData = TestDataUtils.getHoursData();
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", gateData);
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
            } else if (dateRange.equals("1")) {
                map.put("hoursArray", TestDataUtils.getDateArray(7));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 7; j++) {
                        float hoursData = TestDataUtils.getHoursData();
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                List<Float> result = new ArrayList<Float>();
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
                map.put("cumulativeData", map.get("totalValue"));
            }
            if (dateRange.equals("2")) {
                map.put("hoursArray", TestDataUtils.getDateArray(30));
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < 30; j++) {
                        float hoursData = TestDataUtils.getHoursData();
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
            } else if (dateRange.equals("3")) {
                List<String> daysByTime = TestDataUtils.getDaysByTime(dateArray);
                map.put("hoursArray", daysByTime);
                List<Map<String, Object>> gateData = new ArrayList<Map<String, Object>>();
                //按小时查数据
                List<List<Float>> listArray = new ArrayList<List<Float>>();
                for (int i = 0; i < naturalgasGateStations.size(); i++) {
                    float totalGateValue = 0;
                    float minGateValue = 0;
                    Map<String, Object> mapData = new HashMap<String, Object>();
                    List listHourData = new ArrayList();
                    for (int j = 0; j < daysByTime.size(); j++) {
                        float hoursData = TestDataUtils.getHoursData();
                        listHourData.add(hoursData);
                        totalGateValue = totalGateValue + hoursData;
                    }
                    listArray.add(listHourData);
                    mapData.put("gateName", naturalgasGateStations.get(i).getGateName());
                    mapData.put("totalGateValue", totalGateValue);
                    gateData.add(mapData);
                }
                map.put("gateData", TestDataUtils.sortLiet(gateData));
                map.putAll(timeData(listArray.get(0), isAllGate, naturalgasGateStations));
            }
        }
        return ResultUtil.data(map);
    }

    // 数据合并
    public Map<String, Object> timeData(List<Float> list, String isAllGate, List<NaturalgasGateStation> listGateStation) {
        float maxValue = 0;
        float minValue = 0;
        float totalValue = 0;
        float result = 0;
        List<Float> resultList = new ArrayList<>();
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < list.size(); i++) {
            float tempValue = 0;
            if (isAllGate.equals("1")) {
                tempValue = list.get(i) * listGateStation.size();
            } else {
                tempValue = list.get(i);
            }
            if (i == 0) {
                minValue = tempValue;
            }
            if (maxValue < tempValue) {
                maxValue = tempValue;
            }
            if (minValue > tempValue) {
                minValue = tempValue;
            }
            resultList.add(tempValue);
            totalValue = totalValue + tempValue;
        }
        map.put("maxValue", maxValue);
        map.put("minValue", minValue);
        map.put("totalValue", totalValue);
        map.put("result", resultList);
        return map;
    }

    //用量分析数据列表
    @GetMapping("/getAnalysisDataGrid")
    @ResponseBody
    public Result<Object> getAnalysisDataGrid(String category, String dateRange, String[] dateArray, String isAllGate, CustomPage page) {
        Map<String, Object> map = new HashMap<String, Object>();
        NaturalgasGateStation naturalgasGateStation = new NaturalgasGateStation();
        List<NaturalgasGateStation> naturalgasGateStations = iNaturalgasGateStationService.selectList(naturalgasGateStation);
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> analysisDataList = getAnalysisDataList(category, dateRange, dateArray, isAllGate);
        List<Float> resultList = (List<Float>) analysisDataList.get("resultList");
        List<String> listHours = (List<String>) analysisDataList.get("listHours");
        //总用量
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        int bag = 0;
        for (int i = 0; i < listHours.size(); i++) {
            if (bag >= (page.getPageSize() * (page.getPageNumber() - 1)) && bag < (page.getPageSize() * (
                    page.getPageNumber()))) {
                Map<String, Object> row = new HashMap<String, Object>();
                row.put("date", listHours.get(i));
                row.put("value", resultList.get(i));
                list.add(row);
            }
            bag++;
        }
        page.setResultData(list);
        long size = listHours.size();
        page.setTotalRows(size);
        return ResultUtil.data(page);
    }

    //导出分析用量分析表
    @GetMapping("/exportAnalysisData")
    @ResponseBody
    public void exportAnalysisData(HttpServletResponse response, String category, String dateRange, String[] dateArray, String isAllGate,String gateName) throws Exception {
        Map<String, Object> analysisDataList = getAnalysisDataList(category, dateRange, dateArray, isAllGate);
        List<Float> resultList = (List<Float>) analysisDataList.get("resultList");
        List<String> listHours = (List<String>) analysisDataList.get("listHours");
        List<String[]> ll=new ArrayList();
        String[] fields={"时间","流量"};
        for(int i=0;i<resultList.size();i++){
            String[] row=new String[2];
            row[0]=listHours.get(i);
            row[1]=resultList.get(i).toString();
            ll.add(row);
        }
        String fileName="";
        if(isAllGate.equals("1")){
            fileName=gateName+"用量分析";
        }else{
            fileName="站点用量分析";
        }
        HSSFWorkbook book = ExcelUtil.drawExcel(fileName, fields, ll);
        ExcelUtil.downExcel(book,response,fileName);
    }

    public Map<String, Object> getAnalysisDataList(String category, String dateRange, String[] dateArray, String isAllGate) {
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
        List<Float> resultList = new ArrayList<Float>();
        List<String> listHours = new ArrayList<>();
        //总用量
        if (category.equals("0")) {
            if (dateRange.equals("0")) {
                listHours = TestDataUtils.getListHours(dd.format(TestDataUtils.getYesterday()));
                resultList = dataAssembly(listHours, isAllGate, "0");
            } else if (dateRange.equals("1")) {
                listHours = TestDataUtils.getDateArray(7);
                resultList = dataAssembly(listHours, isAllGate, "1");

            } else if (dateRange.equals("2")) {
                listHours = TestDataUtils.getDateArray(30);
                resultList = dataAssembly(listHours, isAllGate, "1");
            } else if (dateRange.equals("3")) {
                List<String> daysByTime = TestDataUtils.getDaysByTime(dateArray);
                resultList = dataAssembly(daysByTime, isAllGate, "1");
            }
            //平均用量
        } else if (category.equals("1")) {
            if (dateRange.equals("0")) {
                listHours.add(dd.format(TestDataUtils.getYesterday()));
                resultList = dataAssembly(listHours, "0", "0");
            } else if (dateRange.equals("1")) {
                listHours = TestDataUtils.getDateArray(7);
                resultList = dataAssembly(listHours, "0", "1");
            } else if (dateRange.equals("2")) {
                listHours = TestDataUtils.getDateArray(30);
                resultList = dataAssembly(listHours, "0", "1");
            } else if (dateRange.equals("3")) {
                listHours = TestDataUtils.getDaysByTime(dateArray);
                resultList = dataAssembly(listHours, "0", "1");
            }
            //平均小时用量
        } else if (category.equals("2")) {
            if (dateRange.equals("0")) {
                listHours.add(dd.format(TestDataUtils.getYesterday()));
                resultList = dataAssembly(listHours, isAllGate, "0");
            } else if (dateRange.equals("1")) {
                listHours = TestDataUtils.getDateArray(7);
                resultList = dataAssembly(listHours, isAllGate, "0");
            } else if (dateRange.equals("2")) {
                listHours = TestDataUtils.getDateArray(30);
                resultList = dataAssembly(listHours, isAllGate, "0");
            } else if (dateRange.equals("3")) {
                listHours = TestDataUtils.getDaysByTime(dateArray);
                resultList = dataAssembly(listHours, isAllGate, "0");
            }
        }
        map.put("resultList", resultList);
        map.put("listHours", listHours);
        return map;
    }

    //数据装配
    public List<Float> dataAssembly(List<String> list, String isAllGate, String isDate) {
        NaturalgasGateStation naturalgasGateStation = new NaturalgasGateStation();
        List<NaturalgasGateStation> naturalgasGateStations = iNaturalgasGateStationService.selectList(naturalgasGateStation);
        List<Float> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (isAllGate.equals("1")) {
                if (isDate.equals("1")) {
                    resultList.add(TestDataUtils.getDateData() * naturalgasGateStations.size());
                } else {
                    resultList.add(TestDataUtils.getHoursData() * naturalgasGateStations.size());
                }
            } else {
                if (isDate.equals("1")) {
                    resultList.add(TestDataUtils.getDateData());
                } else {
                    resultList.add(TestDataUtils.getHoursData());
                }
            }
        }
        return resultList;
    }

}
